package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalServiceOfferConsts;
import com.iwhalecloud.bss.kite.cucc.manager.cache.CuccExtParameterCache;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalAttrHelper;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.LocalIPAddressSourceAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.ztesoft.bss.common.util.ContextUtil;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Objects;

/**
 * <Description> <br>
 *
 * @author cheng.leizhen01<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2021年3月16日 <br>
 * @since R9.0<br>
 * @see com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler <br>
 */
@Service
@AttrHandler(LocalIPAddressSourceAttrHandler.class)
public class LocalIPAddressSourceAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {
    // ip地址来源选择客户自带则IPv4/IPv6地址属性必填
    // ip地址来源选择联通分配则IPv4/IPv6地址属性非必填
    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        String opreatorProvinceNbr= ContextUtil.getLoginInfo().getUserInfo().getPostProvinceNbr();

        if (Objects.isNull(attr) || KiteStringUtils.isEmpty(attr.getValue())) {
            return;
        }
        // IPv4地址
        KiteAttr IPv4Addresses = inst.getAttr("2_0000202");
        // IPv6地址
        KiteAttr IPv6Addresses = inst.getAttr("2_0000203");

        //客户申请IPv4地址数量
        KiteAttr IPv4AddressesNumber = inst.getAttr("2_0000193");
        // 客户申请IPv6地址数量
        KiteAttr IPv6AddressesNumber = inst.getAttr("2_0000194");

        // 1_0000259 客户自带   业务变更场景 且省份为广东省 “51”时  不联动, 业务变更的追单也不联动
        Map<String, Object> loginExtParams = ContextUtil.getLoginInfo().getUserInfo().getExtParams();
        String sceneInitParam = KiteMapUtils.getStrValue(loginExtParams, "upServiceOfferId");
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        String upServiceOfferId = querySceneInstService.getExtParameterValue(data.getSceneInstId(), "upServiceOfferId");
        boolean modifyChaseOrder = LocalServiceOfferConsts.SERVICE_OFFER_FOLLOW.equals(data.getServiceOfferId())
            && (KiteStringUtils.isNotEmpty(sceneInitParam) || KiteStringUtils.isNotEmpty(upServiceOfferId));

        if(!(KiteStringUtils.equals(opreatorProvinceNbr,"51") && (KiteStringUtils.equals(data.getServiceOfferId(),"2826") || modifyChaseOrder))){
            if(KiteStringUtils.equalsAny(attr.getValue(),"1_0000259" )){
                IPv4Addresses.setIsNull(KeyConsts.IFTRUE_F);
                IPv6Addresses.setIsNull(KeyConsts.IFTRUE_F);
                IPv4AddressesNumber.setIsNull(KeyConsts.IFTRUE_T);
                IPv6AddressesNumber.setIsNull(KeyConsts.IFTRUE_T);
            }else {
                IPv4Addresses.setIsNull(KeyConsts.IFTRUE_T);
                IPv6Addresses.setIsNull(KeyConsts.IFTRUE_T);
                IPv4AddressesNumber.setIsNull(KeyConsts.IFTRUE_F);
                IPv6AddressesNumber.setIsNull(KeyConsts.IFTRUE_F);
            }
        }

        //IP地址变更
        KiteAttr IPProtocoVer = inst.getAttr("2_0000047");   //IP协议版本
        //IPV4IP变更类型
        KiteAttr IPv4IPChageType = inst.getAttr("210004178");
        //IPV6IP变更类型
        KiteAttr IPv6IPChageType = inst.getAttr("210004179");


        if((KiteStringUtils.equals(data.getServiceOfferId(),"2826" ) || modifyChaseOrder)
            && !Objects.isNull(IPv4IPChageType) && !Objects.isNull(IPv6IPChageType)){
            KiteAttr IPV6Length = inst.getAttr("200002031");   // IPV6掩码长度   1_0000219 IPv4
            if(!Objects.isNull(IPv4IPChageType.getValue()) && KiteStringUtils.equalsAny(IPProtocoVer.getValue(),"1_0000219" )) {
                //当IP协议版本 选择IPv4
                LocalAttrHelper.showAndNotNull(inst, "210004178");
                if("1_0000258".equals(attr.getValue())){   //联通分配
                    if("110007223".equals(IPv4IPChageType.getValue())){        //增加
                        LocalAttrHelper.showAndNotNull(inst, "210004180");
                        LocalAttrHelper.hideAndNullable(inst, "200002031","210004179","210004181","210004182",
                            "210004183","210004184","210004185","210004186","210004187");

                    } else if("110007224".equals(IPv4IPChageType.getValue())){  //减少
                        LocalAttrHelper.showAndNotNull(inst, "210004182","210004184");
                        LocalAttrHelper.hideAndNullable(inst, "200002031","210004179","210004181",
                            "210004183","210004185","210004186","210004187","210004180");

                    } else if("100007396".equals(IPv4IPChageType.getValue())){  //不变
                        LocalAttrHelper.hideAndNullable(inst, "200002031","210004179","210004181",
                            "210004183","210004185","210004186","210004187","210004180","210004182","210004184");

                    }
                }else if("1_0000259".equals(attr.getValue())){  // 客户自带
                    if("110007223".equals(IPv4IPChageType.getValue())){        //增加
                        LocalAttrHelper.showAndNotNull(inst, "210004180","210004186");
                        LocalAttrHelper.hideAndNullable(inst, "200002031","210004179","210004181","210004182",
                            "210004183","210004184","210004185","210004187");

                    } else if("110007224".equals(IPv4IPChageType.getValue())){  //减少
                        LocalAttrHelper.showAndNotNull(inst, "210004182","210004184");
                        LocalAttrHelper.hideAndNullable(inst, "200002031","210004179","210004181",
                            "210004183","210004185","210004186","210004187","210004180");

                    } else if("100007396".equals(IPv4IPChageType.getValue())) {  //不变
                        LocalAttrHelper.hideAndNullable(inst, "200002031","210004179","210004181",
                            "210004183","210004185","210004186","210004187","210004180","210004182","210004184");
                    }
                }

                // IP协议版本选择 IPV6
            } else if (!Objects.isNull(IPv6IPChageType.getValue()) && KiteStringUtils.equalsAny(IPProtocoVer.getValue(),"1_0000220" )) {
                //当IP协议版本选择IPv6，展示IPv6变更类型
                IPV6Length.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
                LocalAttrHelper.showAndNotNull(inst, "210004179");
                if("1_0000258".equals(attr.getValue())){   //联通分配
                    if("110007223".equals(IPv6IPChageType.getValue())){        //增加
                        LocalAttrHelper.showAndNotNull(inst, "210004181");
                        LocalAttrHelper.hideAndNullable(inst, "210004182","210004180",
                            "210004183","210004184","210004185","210004186","210004187","210004178");

                    } else if("110007224".equals(IPv6IPChageType.getValue())){  //减少
                        LocalAttrHelper.showAndNotNull(inst, "210004183","210004185");
                        LocalAttrHelper.hideAndNullable(inst, "","210004181","210004182","210004184",
                            "210004186","210004187","210004180","210004178");

                    } else if("100007396".equals(IPv6IPChageType.getValue())){  //不变
                        LocalAttrHelper.hideAndNullable(inst, "210004181",
                            "210004183","210004185","210004186","210004187","210004180","210004182","210004184","210004178");
                    }
                }else if("1_0000259".equals(attr.getValue())){  // 客户自带
                    if("110007223".equals(IPv6IPChageType.getValue())){        //增加
                        LocalAttrHelper.showAndNotNull(inst, "210004181","210004187");
                        LocalAttrHelper.hideAndNullable(inst, "210004178","210004180","210004182",
                            "210004183","210004184","210004185","210004186");

                    } else if("110007224".equals(IPv6IPChageType.getValue())){  //减少
                        LocalAttrHelper.showAndNotNull(inst, "210004183","210004185");
                        LocalAttrHelper.hideAndNullable(inst, "210004178","210004181","210004182",
                            "210004184","210004186","210004187","210004180");

                    } else if("100007396".equals(IPv6IPChageType.getValue())) {  //不变
                        LocalAttrHelper.hideAndNullable(inst, "210004178","210004180","210004181",
                            "210004182","210004183","210004184","210004185","210004186","210004187");
                    }
                }

            } else if ((!Objects.isNull(IPv4IPChageType.getValue()) || !Objects.isNull(IPv6IPChageType.getValue()))&&KiteStringUtils.equalsAny(IPProtocoVer.getValue(),"1_0000221")) {
                IPV6Length.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
                LocalAttrHelper.showAndNotNull(inst, "210004178","210004179");
                // IPV4版本协议
                if("1_0000258".equals(attr.getValue())){   //联通分配
                    if("110007223".equals(IPv4IPChageType.getValue())){        //增加
                        LocalAttrHelper.showAndNotNull(inst, "210004180");
                        LocalAttrHelper.hideAndNullable(inst, "210004182","210004184","210004186");

                    } else if("110007224".equals(IPv4IPChageType.getValue())){  //减少
                        LocalAttrHelper.showAndNotNull(inst, "210004182","210004184");
                        LocalAttrHelper.hideAndNullable(inst, "210004180","210004186");

                    } else if("100007396".equals(IPv4IPChageType.getValue())){  //不变
                        LocalAttrHelper.hideAndNullable(inst, "210004180","210004182","210004184","210004186");

                    }
                }else if("1_0000259".equals(attr.getValue())){  // 客户自带
                    if("110007223".equals(IPv4IPChageType.getValue())){        //增加
                        LocalAttrHelper.showAndNotNull(inst, "210004180","210004186");
                        LocalAttrHelper.hideAndNullable(inst, "210004182","210004184");

                    } else if("110007224".equals(IPv4IPChageType.getValue())){  //减少
                        LocalAttrHelper.showAndNotNull(inst, "210004182","210004184");
                        LocalAttrHelper.hideAndNullable(inst, "210004180","210004186");

                    } else if("100007396".equals(IPv4IPChageType.getValue())) {  //不变
                        LocalAttrHelper.hideAndNullable(inst, "210004180","210004182","210004184", "210004186");
                    }
                }

                // IPV6版本协议
                if("1_0000258".equals(attr.getValue())){   //联通分配
                    if("110007223".equals(IPv6IPChageType.getValue())){        //增加
                        LocalAttrHelper.showAndNotNull(inst, "210004181");
                        LocalAttrHelper.hideAndNullable(inst, "210004183","210004185", "210004187");

                    } else if("110007224".equals(IPv6IPChageType.getValue())){  //减少
                        LocalAttrHelper.showAndNotNull(inst, "210004183","210004185");
                        LocalAttrHelper.hideAndNullable(inst, "","210004181","210004187");

                    } else if("100007396".equals(IPv6IPChageType.getValue())){  //不变
                        LocalAttrHelper.hideAndNullable(inst, "210004181","210004183","210004185", "210004187");

                    }
                }else if("1_0000259".equals(attr.getValue())){  // 客户自带
                    if("110007223".equals(IPv6IPChageType.getValue())){        //增加
                        LocalAttrHelper.showAndNotNull(inst, "210004181","210004187");
                        LocalAttrHelper.hideAndNullable(inst, "210004183","210004185");

                    } else if("110007224".equals(IPv6IPChageType.getValue())){  //减少
                        LocalAttrHelper.showAndNotNull(inst, "210004183","210004185");
                        LocalAttrHelper.hideAndNullable(inst, "210004181","210004187");

                    } else if("100007396".equals(IPv6IPChageType.getValue())) {  //不变
                        LocalAttrHelper.hideAndNullable(inst, "210004181","210004183","210004185", "210004187");
                    }
                }
            }
        }

    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }

}
